# -*- coding:iso-8859-1 -*-
#
# Copyright @2009 - Emmanuel Br&ecute;ton
#
# Licensed under the GNU General Public License, Version 3.0 (the
# "License"); you may not use this file except in compliance with the
# License.  You may obtain a copy of the License at the root of this
# project in file LICENSE.TXT
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

try:
    # 0.96
    from django import newforms as forms
    class BaseForm(forms.Form):
        @property
        def cleaned_data(self):
            return self.clean_data

except ImportError:
    # 1.x
    from django import forms
    class BaseForm(forms.Form):
        pass

from django.http import HttpResponse, HttpResponseRedirect, HttpResponseForbidden
from obviews.views import View, FormView, AjaxFormView, CutResponse

# basic form
class TestForm(BaseForm):
    username = forms.CharField(max_length=30)
    password = forms.CharField(max_length=30, widget=forms.PasswordInput)

# simplest form
class SucessView(View):
    template_name = "forms/success.html"
    
class SimplestForm(FormView):
    template_name = "forms/simplest_form.html"
    defaults = {"p": "simplest form",
                "on_success": "/form/success/"}
    form_class = TestForm

    def process_form(self, form, request, **variables):
        pass

# Register
class RegisterForm(FormView):
    template_name = "forms/register_form.html"
    defaults = {"p": "register form",
                "on_success": "/form/registered/"}
    form_class = TestForm

    def process_form(self, form, request, **variables):
        username = form.cleaned_data['username']
        password = form.cleaned_data['password']
        return {"username": username,
                "password": password[0]+'*'*(len(password)-1)}

    def on_success(self, result, request, **variables):
        response = super(RegisterForm, self).on_success(
            result, request, **variables)
        response.set_cookie("username", result['username'])
        response.set_cookie("password", result['password'])
        return response
        
class RegisterView(View):
    template_name = "forms/registered.html"

    def build_params(self, request):
        return request.COOKIES
        
# cutting throug
class CutForm(SimplestForm):
    template_name = "forms/cutting_form.html"
    defaults = {"p": "cutting form"}

    def process_form(self, form, request, **variables):
        if r'\'' in form.cleaned_data["username"] \
               or r'\'' in form.cleaned_data["password"]:
            raise CutResponse(HttpResponseForbidden(
                "Playing around with BAD injection, got a nice 403"))

# Ajax form
class AjaxView(View):
    template_name = "forms/ajax.html"
    defaults = {"p": "ajax form"}
    
class AjaxForm(AjaxFormView):
    template_name = "forms/ajax_form.html"
    defaults = {"on_success": "/form/success/"}
    form_class = TestForm

    def process_form(self, form, request, **variables):
        pass

# all inclusive
class AllFormView(View):
    # this page
    template_name = "forms/all.html"
    defaults = {"p": "more hooks"}
    
class ColorForm(BaseForm):
    # the form
    integer = forms.IntegerField()

class ResultsView(View):
    # page when JS not activated
    template_name = "forms/results.html"

    def build_params(self, request, integer=None):
        return {"integer": int(integer),
                "color": "#%06x"% (int(integer)%16**6)}
    
class AllForm(AjaxFormView):
    # the form view (with or without JS)
    template_name = "forms/all_form.html"
    form_class = ColorForm

    def process_form(self, form, request, **variables):
        return form.cleaned_data["integer"]

    def ajax_success(self, result, request, **built_kwargs):
        return HttpResponse("#%06x"% (int(result)%16**6))
    
    def noajax_success(self, result, request, **built_kwargs):
        return HttpResponseRedirect("/form/all/results/%s/"% result)

# use cases... comment
class AddingCommentView(View):
    template_name = "forms/comment_form.html"
    defaults = {"p": "comments"}
